package com.shj.em.biz.sys.controller;

import com.shj.em.util.DateUtils;
import com.shj.em.util.StringUtils;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.sys.entity.EmpAmountConf;
import com.shj.em.biz.sys.service.EmpAmountConfService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 员工单位时间金额表 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-30
 */
@Controller
@Slf4j
@RequestMapping("/sys/empAmountConf")
public class EmpAmountConfController extends BaseController<EmpAmountConf>{

    private String prefix = "sys/empamountconf";

    @Autowired
    EmpAmountConfService empamountconfService;

    @GetMapping("/gotoSearchEmp")
    String gotoSearchEmp(){
        return prefix + "/search";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageVO<EmpAmountConf> list(@RequestBody Map<String, Object> params){
        Page<EmpAmountConf> page = empamountconfService.queryEmpAmountConfPage(params);
        PageVO<EmpAmountConf> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(page.getRecords());
        return pageVO;
    }

    @GetMapping("/gotoAdd")
	String gotoAdd(Model model){
        model.addAttribute("isEdit", false);
        model.addAttribute("empamountconf", new EmpAmountConf());
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        EmpAmountConf empamountconf = empamountconfService.selectById(id);
		model.addAttribute("empamountconf", empamountconf);
        model.addAttribute("isEdit", true);
	    return prefix + "/add";
	}

	/*@PostMapping("/saveEmpAmount")
    @ResponseBody
    public ResMsg saveEmpAmount(Long deptId, String empAmounts){
        log.info(empAmounts);
        String[] nameValArr = empAmounts.split(";");
        List<EmpAmountConf> empAmountConfs = new ArrayList<>();

        Map<String, EmpAmountConf> empExistMap = new HashMap<>();
        EmpAmountConf conf;
        for(int i = 0; i < nameValArr.length; i++){
            String key = nameValArr[i].substring(0, nameValArr[i].lastIndexOf("."));
            if(empExistMap.containsKey(key)){
                conf = empExistMap.get(key);
            }else{
                conf = new EmpAmountConf();
                empAmountConfs.add(conf);
                empExistMap.put(key, conf);
            }

            String[] nameVal = nameValArr[i].split("=");
            String value = nameVal.length == 1 ? null : nameVal[1];
            String name = nameVal[0].substring(nameVal[0].lastIndexOf(".") + 1);

            if("id".equals(name)){
                conf.setId(value == null ? null : Long.parseLong(value));
            }else if("empId".equals(name)){
                conf.setEmpId(value == null ? null : Long.parseLong(value));
            }else if("min".equals(name)){
                conf.setMin(value == null ? null : new BigDecimal(value));
            }else if("max".equals(name)){
                conf.setMax(value == null ? null : new BigDecimal(value));
            }else if("defineDt".equals(name)){
                Date today = new Date();
                if(StringUtils.isBlank(value)){
                    conf.setFlag("I");
                    conf.setDefineDt(today);
                }else{
                    String todayStr = DateUtils.format(today, DateUtils.DATE_PATTERN);
                    conf.setDefineDt(today);
                    if(todayStr.equals(value)){
                        conf.setFlag("U");
                    }else{
                        conf.setFlag("D");
                    }
                }
            }
        }
        if(empAmountConfs.isEmpty()){
            return ResMsg.error("没有记录可以保存");
        }

        for(EmpAmountConf amountConf : empAmountConfs){
            if("D".equals(amountConf.getFlag())){
                empamountconfService.deleteById(amountConf.getId());
                empamountconfService.insert(amountConf);
            }else if("I".equals(amountConf.getFlag())){
                empamountconfService.insert(amountConf);
            }else{
                empamountconfService.updateById(amountConf);
            }
        }

        List<EmpAmountConf> list = empamountconfService.findEmpAmountConfsByDeptId(deptId);
        Map<String, EmpAmountConf> ids = new HashMap<>();
        for(EmpAmountConf empConf : list){
            ids.put("emp" + empConf.getEmpId(), empConf);
        }

        return ResMsg.ok(ids);
    }*/

	@Log("创建员工单位时间金额表")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(EmpAmountConf empamountconf){
        try{
            empamountconfService.insert(empamountconf);
            return ResMsg.ok(new EmpAmountConf());
        }catch(Exception e){
            log.error("创建员工单位时间金额表失败", e);
            return ResMsg.error("创建员工单位时间金额表失败");
        }

	}

    @Log("更新员工单位时间金额表")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(EmpAmountConf empamountconf){
        try{
            empamountconfService.updateById(empamountconf);
            return ResMsg.ok();
        }catch(Exception e){
            log.error("更新员工单位时间金额表失败", e);
            return ResMsg.error("更新员工单位时间金额表失败");
        }

	}

	@Log("删除员工单位时间金额表")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            empamountconfService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

	@Log("批量删除员工单位时间金额表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        empamountconfService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

